package com.donlim.dev.component;

import com.donlim.dev.dto.CommonResult;
import com.donlim.dev.enums.ResultCode;
import com.donlim.dev.exception.SysUserException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by 星 on 2020/11/3
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public CommonResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e){
        return CommonResult.failed(ResultCode.VALIDATE_FAILED.getCode(),e.getBindingResult().getFieldError().getDefaultMessage());
    }

    @ExceptionHandler(SysUserException.class)
    @ResponseBody
    public CommonResult handleSysUserException(SysUserException e){
        return CommonResult.failed(e.getCode(),e.getMessage());
    }
}
